// pages/evaluating/exam/exam.js
const App = getApp()
// let AJAX = require("../../../utils/util.js").AJAX
let { AJAX, imgUrl } = require("../../../utils/util.js")

Page({
 
	/**
	 * 页面的初始数据
	 */
	data: {
		navH: '',
		examList: [],
		curSwiper: 0,
		answerData: [],
    imgUrl: imgUrl
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			navH: App.globalData.navHeight
		})
		this.requestExams()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},
	//
	requestExams() {
		AJAX('question-guide/list').then( res => {
			this.setData({
				examList: res.data
			})
		})
	},

	//
	catchTouchMove() {
		return false
	},
	//提交测试；
	subExam() {
		AJAX('question-guide/evaluating', 'POST', {
			answer: this.data.answerData.join(',')
		}).then( res =>  {
			wx.showToast({
				title: res.message ? res.message : '提交成功',
				duration: 1000,
				success: () => {
					// setTimeout()
					// wx.navigateTo({
					// 	url: "/pages/evaluating/result/result",
					// })
				},
				complete: () => {
					wx.navigateTo({
						url: "/pages/evaluating/result/result",
					})
				}
			})
		})
		
	},
	examClickEvent(e) {
		let _s_index = e.currentTarget.dataset.s_index
		let cur_index = this.data.curSwiper
		if (this.timer) {
			clearTimeout(this.timer)
		}
		let arr = this.data.answerData
		arr[cur_index] = this.data.examList[cur_index].answers[_s_index].id
		this.setData({
			answerData: arr,
		})
		if (cur_index + 1 == this.data.examList.length) return
		this.timer = setTimeout(() => {
			this.setData({
				curSwiper: ++cur_index
			})
		}, 500)
	},
   navBack(){
      wx.navigateTo({
         url: '/pages/evaluating/evaluating',
      })
   }
})